package com.stylefeng.guns.modular.vpn_pay_method.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.constant.DatasourceEnum;
import com.stylefeng.guns.core.mutidatasource.annotion.DataSource;
import com.stylefeng.guns.modular.vpn.dto.PayMethodDTO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.modular.system.model.Paymethod;
import com.stylefeng.guns.modular.vpn_pay_method.service.IPaymethodService;

/**
 * 支付方式管理控制器
 *
 * @author fengshuonan
 * @Date 2020-03-26 10:27:23
 */
@Controller
@RequestMapping("/paymethod")
public class PaymethodController extends BaseController {

    private String PREFIX = "/vpn_pay_method/paymethod/";

    @Autowired
    private IPaymethodService paymethodService;

    /**
     * 跳转到支付方式管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "paymethod.html";
    }

    /**
     * 跳转到添加支付方式管理
     */
    @RequestMapping("/paymethod_add")
    public String paymethodAdd() {
        return PREFIX + "paymethod_add.html";
    }

    /**
     * 跳转到修改支付方式管理
     */
    @RequestMapping("/paymethod_update/{paymethodId}")
    @DataSource(name = DatasourceEnum.DATA_SOURCE_BIZ)
    public String paymethodUpdate(@PathVariable Integer paymethodId, Model model) {
        Paymethod paymethod = paymethodService.selectById(paymethodId);
        model.addAttribute("item",paymethod);
        LogObjectHolder.me().set(paymethod);
        return PREFIX + "paymethod_edit.html";
    }

    /**
     * 获取支付方式管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    @DataSource(name = DatasourceEnum.DATA_SOURCE_BIZ)
    public Object list(String condition) {
        EntityWrapper<Paymethod> wrapper = new EntityWrapper<>();
        wrapper.orderBy("platform",true);
        return paymethodService.selectList(wrapper);
    }

    /**
     * 新增支付方式管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(PayMethodDTO paymethod) {
        paymethodService.addPayMethod(paymethod);
        return SUCCESS_TIP;
    }

    /**
     * 删除支付方式管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @DataSource(name = DatasourceEnum.DATA_SOURCE_BIZ)
    public Object delete(@RequestParam Integer paymethodId) {
        paymethodService.deleteById(paymethodId);
        return SUCCESS_TIP;
    }

    /**
     * 修改支付方式管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @DataSource(name = DatasourceEnum.DATA_SOURCE_BIZ)
    public Object update(Paymethod paymethod) {
        paymethodService.updateById(paymethod);
        return SUCCESS_TIP;
    }

    /**
     * 支付方式管理详情
     */
    @RequestMapping(value = "/detail/{paymethodId}")
    @ResponseBody
    @DataSource(name = DatasourceEnum.DATA_SOURCE_BIZ)
    public Object detail(@PathVariable("paymethodId") Integer paymethodId) {
        return paymethodService.selectById(paymethodId);
    }
}
